{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# while循环\n",
    "- 一个循环语句\n",
    "- 表示当某条件成立的时候，就循环\n",
    "- 不知道具体循环次数，但能确定循环的成立条件的时候用while循环\n",
    "- while语法：\n",
    "    \n",
    "        while 条件表达式：\n",
    "            语句块\n",
    "            \n",
    "        #另外一种表达方法\n",
    "        while  条件表达式：\n",
    "            语句块1\n",
    "        else：\n",
    "            语句块2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第 1 年拿了 106700.0 块钱\n",
      "第 2 年拿了 113848.9 块钱\n",
      "第 3 年拿了 121476.77629999998 块钱\n",
      "第 4 年拿了 129615.72031209998 块钱\n",
      "第 5 年拿了 138299.97357301068 块钱\n",
      "第 6 年拿了 147566.07180240238 块钱\n",
      "第 7 年拿了 157452.99861316333 块钱\n",
      "第 8 年拿了 168002.34952024528 块钱\n",
      "第 9 年拿了 179258.5069381017 块钱\n",
      "第 10 年拿了 191268.8269029545 块钱\n",
      "第 11 年拿了 204083.83830545243 块钱\n"
     ]
    }
   ],
   "source": [
    "# 如果说年利率是6.7%，本利是每年翻滚， 则多少年后本钱会翻倍\n",
    "\n",
    "benqian = 100000\n",
    "year = 0\n",
    "while benqian < 200000:\n",
    "    benqian = benqian * (1+0.067)\n",
    "    year += 1 # year = year + 1\n",
    "    print(\"第 {0} 年拿了 {1} 块钱\".format(year, benqian))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第 1 年拿了 106700.0 块钱\n",
      "第 2 年拿了 113848.9 块钱\n",
      "第 3 年拿了 121476.77629999998 块钱\n",
      "第 4 年拿了 129615.72031209998 块钱\n",
      "第 5 年拿了 138299.97357301068 块钱\n",
      "第 6 年拿了 147566.07180240238 块钱\n",
      "第 7 年拿了 157452.99861316333 块钱\n",
      "第 8 年拿了 168002.34952024528 块钱\n",
      "第 9 年拿了 179258.5069381017 块钱\n",
      "第 10 年拿了 191268.8269029545 块钱\n",
      "第 11 年拿了 204083.83830545243 块钱\n",
      "大爷的，终于翻倍了，10多年呀\n",
      "当年10万可以盖个房子，现在儿子结婚，20万可以给他盖个厨房了\n"
     ]
    }
   ],
   "source": [
    "# 如果说年利率是6.7%，本利是每年翻滚， 则多少年后本钱会翻倍\n",
    "# 如果拿到的钱翻倍，则用print庆祝一下\n",
    "benqian = 100000\n",
    "year = 0\n",
    "while benqian < 200000:\n",
    "    benqian = benqian * (1+0.067)\n",
    "    year += 1 # year = year + 1\n",
    "    print(\"第 {0} 年拿了 {1} 块钱\".format(year, benqian))\n",
    "else:\n",
    "    print(\"大爷的，终于翻倍了，10多年呀\")\n",
    "    print(\"当年10万可以盖个房子，现在儿子结婚，20万可以给他盖个厨房了\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数\n",
    "- 代码的一种组织形式\n",
    "- 一个函数一般完成一项特定的功能\n",
    "- 函数使用\n",
    "    - 函数需要先定义\n",
    "    - 使用函数，俗称调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我结束了\n"
     ]
    }
   ],
   "source": [
    "# 定义一个函数\n",
    "# 只是定义的话不会执行\n",
    "# 1. def关键字，后跟一个空格\n",
    "# 2. 函数名，自己定义，起名需要遵循便令命名规则，约定俗成，大驼峰命名只给类用\n",
    "# 3. 后面括号和冒号不能省，括号内可以由参数\n",
    "# 4. 函数内所有代码缩进\n",
    "\n",
    "def func():\n",
    "    print(\"我是一个函数\")\n",
    "    print(\"我要完成一定功能\")\n",
    "print(\"我结束了\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是一个函数\n",
      "我要完成一定功能\n"
     ]
    }
   ],
   "source": [
    "# 函数的调用\n",
    "# 直接函数名后面跟括号\n",
    "func()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 函数的参数和返回值\n",
    "- 参数： 负责给函数传递一些必要的数据或者信息\n",
    "    - 形参（形式参数）： 在函数定义的时候用到的参数没有具体值，只是一个占位的符号，成为形参\n",
    "    - 实参（实际参数）： 在调用函数的时候输入的值\n",
    "- 返回值： 函数的执行结果\n",
    "    - 使用return关键字\n",
    "    - 如果没有return，默认返回一个None\n",
    "    - 函数一旦执行return语句，则无条件返回，即结束函数的执行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "明月, 你肿么咧\n",
      "Sir, 你不理额额就走咧\n"
     ]
    }
   ],
   "source": [
    "# 参数的定义和使用\n",
    "# 参数person只是一个符号，代表的是调用的时候的某一个数据\n",
    "# 调用的时候，会用p的值代替函数中所有的person\n",
    "\n",
    "def hello(person):\n",
    "    print(\"{0}, 你肿么咧\".format(person))\n",
    "    print(\"Sir, 你不理额额就走咧\")\n",
    "  \n",
    "p = \"明月\"\n",
    "hello(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "明月, 你肿么咧\n",
      "Sir, 你不理额额就走咧\n",
      "我已经跟明月打招呼了，明月不理我\n"
     ]
    }
   ],
   "source": [
    "# return语句的基本使用\n",
    "# 函数打完招呼后返回一句话\n",
    "def hello(person):\n",
    "    print(\"{0}, 你肿么咧\".format(person))\n",
    "    print(\"Sir, 你不理额额就走咧\")\n",
    "    \n",
    "    return \"我已经跟{0}打招呼了，{1}不理我\".format(person, person)\n",
    "  \n",
    "p = \"明月\"\n",
    "rst = hello(p)\n",
    "\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LiYing, 你肿么咧\n",
      "哈哈，我提前结束了\n"
     ]
    }
   ],
   "source": [
    "# return案例2\n",
    "\n",
    "def hello(person):\n",
    "    print(\"{0}, 你肿么咧\".format(person))\n",
    "    return \"哈哈，我提前结束了\"\n",
    "    print(\"Sir, 你不理额额就走咧\")\n",
    "    return \"我已经跟{0}打招呼了，{1}不理我\".format(person, person)\n",
    "\n",
    "p = \"LiYing\"\n",
    "rst = hello(p)\n",
    "print(rst)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function print in module builtins:\n",
      "\n",
      "print(...)\n",
      "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "    \n",
      "    Prints the values to a stream, or to sys.stdout by default.\n",
      "    Optional keyword arguments:\n",
      "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "    sep:   string inserted between values, default a space.\n",
      "    end:   string appended after the last value, default a newline.\n",
      "    flush: whether to forcibly flush the stream.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 查找函数帮助文档\n",
    "# 1. 用help函数\n",
    "help(print)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 -------------------\n",
      "2 4 -------------------\n",
      "3 6 9 -------------------\n",
      "4 8 12 16 -------------------\n",
      "5 10 15 20 25 -------------------\n",
      "6 12 18 24 30 36 -------------------\n",
      "7 14 21 28 35 42 49 -------------------\n",
      "8 16 24 32 40 48 56 64 -------------------\n",
      "9 18 27 36 45 54 63 72 81 -------------------\n"
     ]
    }
   ],
   "source": [
    "# 九九乘法表\n",
    "# version 1.0\n",
    "for row in range(1,10):\n",
    "    # 打印一行\n",
    "    for col in range(1, row+1):\n",
    "        # print函数默认任务打印完毕后换行\n",
    "        print( row * col, end=\" \")\n",
    "    print(\"-------------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 \n",
      "2 4 \n",
      "3 6 9 \n",
      "4 8 12 16 \n",
      "5 10 15 20 25 \n",
      "6 12 18 24 30 36 \n",
      "7 14 21 28 35 42 49 \n",
      "8 16 24 32 40 48 56 64 \n",
      "9 18 27 36 45 54 63 72 81 \n"
     ]
    }
   ],
   "source": [
    "# 定义一个函数，打印一行九九乘法表\n",
    "def printLine(row):\n",
    "    for col in range(1, row+1):\n",
    "        # print函数默认任务打印完毕后换行\n",
    "        print( row * col, end=\" \")\n",
    "    print(\"\")\n",
    "  \n",
    "# 九九乘法表\n",
    "# version 2.0\n",
    "for row in range(1,10):\n",
    "    printLine(row)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 参数详解\n",
    "- [参考资料](https://www.cnblogs.com/bingabcd/p/6671368.html)\n",
    "- python参考资料： headfirst python -> 零基础入门学习python（小甲鱼),本讲义参考的是流畅的python-->习题-->后期可以考虑腾讯公开免费课\n",
    "- 参数分类\n",
    "    - 普通参数\n",
    "    - 默认参数\n",
    "    - 关键字参数\n",
    "    - 收集参数\n",
    "\n",
    "- 普通参数\n",
    "    - 参见上例\n",
    "    - 定义的时候直接定义变量名\n",
    "    - 调用的时候直接把变量或者值放入指定位置\n",
    "    \n",
    "    \n",
    "            \n",
    "                def 函数名（参数1， 参数2，....):\n",
    "                    函数体\n",
    "                    \n",
    "                # 调用\n",
    "                函数名（value1, value2,.......)\n",
    "                \n",
    "                # 调用的时候，具体值参考的是位置，按位置赋值\n",
    "            \n",
    "- 默认参数\n",
    "    - 形参带有默认值\n",
    "    - 调用的时候，如果没有对相应形参赋值，则使用默认值\n",
    "    \n",
    "                def  func_name(p1=v1, p2=v2..........):\n",
    "                    func_block\n",
    "                    \n",
    "                #调用1\n",
    "                func_name()\n",
    "                \n",
    "                # 调用2\n",
    "                value1= 100\n",
    "                value2=200\n",
    "                func_name(value1,value2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 默认参数示例\n",
    "# 报名函数，需要知道学生性别\n",
    "# 学习python的学生基本都是男生，所以，报名的时候如果没有特别指定，我们任务是男生\n",
    "def reg(name, age, gender=\"male\"):\n",
    "    if gender == \"male\":\n",
    "        print(\"{0} is {1}, and he is a good student\".format(name, age))\n",
    "    else:\n",
    "        print(\"{0} is {1}, and she is a good student\".format(name, age))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mingyue is 21, and he is a good student\n",
      "xiaojing is 23, and she is a good student\n"
     ]
    }
   ],
   "source": [
    "# 调用默认参数函数案例1\n",
    "\n",
    "reg(\"mingyue\", 21)\n",
    "\n",
    "reg(\"xiaojing\", 23, \"female\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
